#
# This file is run prior to the unit tests for preparing the testing
# environment. Currently only required / necessary for running the
# tests on the Android device emulator.
#

# Restore the required settings of the CMake configuration step
cmake_minimum_required(VERSION 3.1)
set(CMAKE_COMMAND "@CMAKE_COMMAND@")
set(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
set(CMAKE_CURRENT_BINARY_DIR "@CMAKE_BINARY_DIR@")
set(CMAKE_FILES_DIRECTORY "@CMAKE_FILES_DIRECTORY@")
set(CMAKE_MODULE_PATH "@CMAKE_ROOT@/Modules")
set(CMAKE_SHARED_LIBRARY_PREFIX "@CMAKE_SHARED_LIBRARY_PREFIX@")
set(CMAKE_SHARED_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@")
set(CMAKE_HOST_SYSTEM "@CMAKE_HOST_SYSTEM@")
set(DCMTK_CMAKE_INCLUDE "@CMAKE_SOURCE_DIR@/@DCMTK_CMAKE_INCLUDE@")
set(DCMTK_TEST_EXECUTABLES "@DCMTK_TEST_EXECUTABLES@")
set(DCMTK_ALL_LIBRARIES "@DCMTK_ALL_LIBRARIES@")
set(DCMTK_LIBRARY_DEPENDENCIES "@DCMTK_LIBRARY_DEPENDENCIES@")
set(DCMTK_PACKAGE_VERSION "@DCMTK_PACKAGE_VERSION@")
set(DCMTK_ABI_VERSION "@DCMTK_ABI_VERSION@")
set(BUILD_SHARED_LIBS "@BUILD_SHARED_LIBS@")
set(DCMTK_DICOM_DICTIONARIES "@DCMTK_DICOM_DICTIONARIES@")
set(ANDROID "@ANDROID@")
set(ANDROID_ADB_PROGRAM "@ANDROID_ADB_PROGRAM@")
set(ANDROID_ANDROID_PROGRAM "@ANDROID_ANDROID_PROGRAM@")
set(ANDROID_EMULATOR_PROGRAM "@ANDROID_EMULATOR_PROGRAM@")
set(ANDROID_EMULATOR_AVD "@ANDROID_EMULATOR_AVD@")
set(ANDROID_RUNTIME_LIBRARIES "@ANDROID_RUNTIME_LIBRARIES@")
set(ANDROID_TEMPORARY_FILES_LOCATION "@ANDROID_TEMPORARY_FILES_LOCATION@")
# Emulate some required CMake commands while running inside CTest
include(${DCMTK_CMAKE_INCLUDE}CMake/CTest/dcmtkCTestMacros.cmake)
# For DCMTK_UNSET_XXX
include(${DCMTK_CMAKE_INCLUDE}CMake/dcmtkMacros.cmake)
# Load required functions for controlling the Android device emulator
include(${DCMTK_CMAKE_INCLUDE}CMake/dcmtkUseAndroidSDK.cmake)

# Prepare the Android testing environment
# Prepare the emulator
DCMTK_ANDROID_START_EMULATOR(DCMTK_ANDROID_EMULATOR_INSTANCE)
DCMTK_ANDROID_WAIT_FOR_EMULATOR(DCMTK_ANDROID_EMULATOR_INSTANCE)

# Get the emulator's name for emitting a shutdown command when done
DCMTK_ANDROID_GET_OBJECT_PROPERTIES(DCMTK_ANDROID_EMULATOR_INSTANCE)
DCMTK_ANDROID_STOP_EMULATOR_COMMAND(STOP_EMULATOR "${EMULATOR_NAME}")
DCMTK_ANDROID_EMULATOR_SHUTDOWN_MESSAGE(STOP_MESSAGE)
# Note: no quotation marks around STOP_MESSAGE, since it contains the message
#       and the message type (e.g. STATUS)
MESSAGE_COMMAND(STOP_MESSAGE ${STOP_MESSAGE})
# CTEST_CUSTOM_POST_TESTS executes a sequence of 'non CMake' commands separated
# by ';' characters. Therefore the sequence a single command consists of must
# be translated appropriately first
string(REPLACE ";" " " STOP_EMULATOR "${STOP_EMULATOR}")
string(REPLACE ";" " " STOP_MESSAGE "${STOP_MESSAGE}")
# Register the cleanup hooks
set(CTEST_CUSTOM_POST_TEST "${STOP_MESSAGE}" "${STOP_EMULATOR}")

# Set the current emulator instance handle via an environment variable
set(ENV{DCMTK_ANDROID_EMULATOR_INSTANCE} "${DCMTK_ANDROID_EMULATOR_INSTANCE}")

# Collect the list of required libraries
if(BUILD_SHARED_LIBS)
    foreach(LIBRARY ${DCMTK_ALL_LIBRARIES})
        list(APPEND DCMTK_CREATED_SHARED_LIBRARIES "${CMAKE_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_PACKAGE_VERSION}")
    endforeach()
endif()

# Transmit the required executables, libraries and dictionaries to the emulator
DCMTK_ANDROID_PUSH(DCMTK_ANDROID_EMULATOR_INSTANCE
    ${ANDROID_RUNTIME_LIBRARIES}
    ${DCMTK_LIBRARY_DEPENDENCIES}
    ${DCMTK_CREATED_SHARED_LIBRARIES}
    ${DCMTK_TEST_EXECUTABLES}
    ${DCMTK_DICOM_DICTIONARIES}
    DESTINATION ${ANDROID_TEMPORARY_FILES_LOCATION}
)

# Set executable permissions
foreach(TEST_EXECUTABLE ${DCMTK_TEST_EXECUTABLES})
    get_filename_component(NAME "${TEST_EXECUTABLE}" NAME)
    DCMTK_ANDROID_SHELL(DCMTK_ANDROID_EMULATOR_INSTANCE
        COMMAND chmod 755 "${ANDROID_TEMPORARY_FILES_LOCATION}/${NAME}"
        OUTPUT_QUIET
        ERROR_QUIET
    )
endforeach()

# Create necessary softlinks
if(BUILD_SHARED_LIBS)
    foreach(LIBRARY ${DCMTK_ALL_LIBRARIES})
        DCMTK_ANDROID_SHELL(DCMTK_ANDROID_EMULATOR_INSTANCE
        COMMAND ln -s "./${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_PACKAGE_VERSION}" "${ANDROID_TEMPORARY_FILES_LOCATION}/${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_ABI_VERSION}"
        OUTPUT_QUIET
        ERROR_QUIET
        )
    endforeach()
endif()
